using HybridCLR.Editor;
using UnityEditor;
using UnityEngine;

namespace GameMain.Editor.BuildPipeline
{
    public partial class BuildPipeline
    {
        private static bool CheckPlatform(BuildTarget target)
        {
            if (target == EditorUserBuildSettings.activeBuildTarget)
            {
                return true;
            }

            Debug.LogError($"[BuildPipeline::CheckPlatform] 请先切到{target}平台再打包");

            return false;
        }

        private static void Prepare()
        {
            GenerateDllActiveBuildTarget();
            BuildSceneAssetBundleActiveBuildTarget();
        }

        [MenuItem("HybridCLR/Build/ActiveBuildTarget")]
        private static void Build_ActiveBuild()
        {
            switch (EditorUserBuildSettings.activeBuildTarget)
            {
                case BuildTarget.Android:
                    Build_Android64();
                    break;
                case BuildTarget.StandaloneWindows64:
                    Build_Win64();
                    break;
                case BuildTarget.StandaloneOSX:
                    Build_MacOSX();
                    break;
                default:
                    Debug.LogError($"[BuildPipeline::CheckPlatform] 请先切到合适平台再打包");
                    break;
            }
        }

        [MenuItem("HybridCLR/Build/Win64")]
        private static void Build_Win64()
        {
            Debug.Log($"[BuildPipeline::Build_Win64] Begin");
            if (!CheckPlatform(BuildTarget.StandaloneWindows64))
            {
                Debug.Log($"[BuildPipeline::Build_Win64] End");
                return;
            }

            Prepare();
            
            var outputPath = $"{SettingsUtil.ProjectDir}/Release-Win64";
            var location = $"{outputPath}/HybridCLRXLua.exe";

            Debug.Log($"[BuildPipeline::Build_Win64] BuildPlayer");
            
            var report = UnityEditor.BuildPipeline.BuildPlayer(new BuildPlayerOptions
            {
                scenes = new[] {"Assets/Scenes/GameMain.unity"},
                locationPathName = location,
                options = BuildOptions.None,
                target = BuildTarget.StandaloneWindows64,
                targetGroup = BuildTargetGroup.Standalone,
            });
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.Log($"[BuildPipeline::Build_Win64] BuildPlayer 打包失败:{report.summary.result}");
                return;
            }

            Debug.Log($"[BuildPipeline::Build_Win64] End");
#if UNITY_EDITOR
            Application.OpenURL($"file:///{location}");
#endif
        }

        [MenuItem("HybridCLR/Build/Mac Os")]
        private static void Build_MacOSX()
        {
            Debug.Log($"[BuildPipeline::Build_MacOSX] Begin");
            if (!CheckPlatform(BuildTarget.StandaloneOSX))
            {
                Debug.Log($"[BuildPipeline::Build_MacOSX] End");
                return;
            }

            Prepare();
            
            var outputPath = $"{SettingsUtil.ProjectDir}/Release-MacOSX";
            var location = $"{outputPath}/HybridCLRXLua.app";

            Debug.Log($"[BuildPipeline::Build_MacOSX] BuildPlayer");
            
            var report = UnityEditor.BuildPipeline.BuildPlayer(new BuildPlayerOptions
            {
                scenes = new[] {"Assets/Scenes/GameMain.unity"},
                locationPathName = location,
                options = BuildOptions.None,
                target = BuildTarget.StandaloneOSX,
                targetGroup = BuildTargetGroup.Standalone,
            });
            
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.Log($"[BuildPipeline::Build_MacOSX] BuildPlayer 打包失败:{report.summary.result}");
                return;
            }

            Debug.Log($"[BuildPipeline::Build_MacOSX] End");
#if UNITY_EDITOR
            Application.OpenURL($"file:///{location}");
#endif
        }

        [MenuItem("HybridCLR/Build/Android64")]
        private static void Build_Android64()
        {
            Debug.Log($"[BuildPipeline::Build_Android64] Begin");
            if (!CheckPlatform(BuildTarget.Android))
            {
                Debug.Log($"[BuildPipeline::Build_Android64] End");
                return;
            }
            
            Prepare();

            var outputPath = $"{SettingsUtil.ProjectDir}/Release-Android/";
            var location = outputPath + "/HybridCLRXLua.apk";

            Debug.Log($"[BuildPipeline::Build_Android64] BuildPlayer");
            
            UnityEditor.BuildPipeline.BuildPlayer(new BuildPlayerOptions
            {
                scenes = new[] {"Assets/Scenes/GameMain.unity"},
                locationPathName = location,
                options = BuildOptions.None,
                target = BuildTarget.Android,
                targetGroup = BuildTargetGroup.Android,
            });

            Debug.Log($"[BuildPipeline::Build_Android64] End");
#if UNITY_EDITOR
            Application.OpenURL($"file:///{outputPath}");
#endif
        }
    }
}
